package org.budo.weixin.api;

import java.io.Serializable;
import java.util.Map.Entry;

import org.budo.support.lang.util.StringUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author li
 * @version 1 (2015年6月26日 下午4:09:03)
 * @since Java7
 *        {"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
 *        {"errcode":40004,"errmsg":"invalid media type"}
 */
@SuppressWarnings({ "rawtypes" })
public class WeixinMediaUploadResult implements Serializable {
    private static final long serialVersionUID = -3932502926415928694L;

    private Boolean success;

    private String type;

    private String mediaId;

    private String thumbMediaId;

    private Long createdAt;

    private Integer errorCode;

    private String errorMessage;

    private String response;

    public WeixinMediaUploadResult(String response) {
        this.response = response;

        if (StringUtil.isEmpty(response)) {
            setSuccess(false);
            setErrorCode(-1);
            setErrorMessage("response is empty");
        } else if (!StringUtil.startWith(response, "{") || !StringUtil.endWith(response, "}")) {
            setSuccess(false);
            setErrorCode(-2);
            setErrorMessage("response is not json, " + response);
        } else {
            JSONObject jsonMap = JSON.parseObject(response);
            Integer errcode = jsonMap.getInteger("errcode");
            String mediaId = jsonMap.getString("media_id");
            String mediaType = jsonMap.getString("type");
            String thumbMediaId = jsonMap.getString("thumb_media_id");
            if (null != mediaId && null != mediaType) {
                setSuccess(true);
                setType(mediaType);
                setMediaId(mediaId);
                setThumbMediaId(get_thumb_media_id(jsonMap)); //{"type":"thumb","thumb_media_id":"vsr0IezNsyEUImELfykTsNKZS2UGGQ3zKJcb9bhFb1sWZkhCFOx0WOmPPBI6RYUR","created_at":1436358065}
                setCreatedAt(jsonMap.getLong("created_at"));
            } else if (null != thumbMediaId && null != mediaType) {
                setSuccess(true);
                setType(mediaType);
                setThumbMediaId(thumbMediaId);
                setCreatedAt(jsonMap.getLong("created_at"));
            } else {
                setSuccess(false);
                setErrorCode(errcode);
                setErrorMessage(jsonMap.getString("errmsg"));
            }
        }
    }

    private String get_thumb_media_id(JSONObject jsonMap) {
        for (Object each : jsonMap.entrySet()) {
            Entry entry = (Entry) each;
            if (StringUtil.equalsIgnoreCase("thumb_media_id", (String) entry.getKey())) {
                return (String) entry.getValue();
            }
        }
        return null;
    }

    public String getThumbMediaId() {
        return thumbMediaId;
    }

    public void setThumbMediaId(String thumbMediaId) {
        this.thumbMediaId = thumbMediaId;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Long getCreatedAt() {
        return createdAt;
    }

    public Integer getErrorCode() {
        return errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public String getMediaId() {
        return mediaId;
    }

    public String getType() {
        return type;
    }

    public void setCreatedAt(Long createdAt) {
        this.createdAt = createdAt;
    }

    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public void setMediaId(String mediaId) {
        this.mediaId = mediaId;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String toString() {
        return super.toString() + ", response=" + response;
    }
}